/**
 * 55. 跳跃游戏
 * https://leetcode-cn.com/problems/jump-game/
 */

/**
 * @param {number[]} nums
 * @return {boolean}
 */
function canJump(nums) {
  const { length } = nums;
  let maxIndex = 0;
  const lastIndex = length - 1;
  for (let i = 0; i < lastIndex; i += 1) {
    if (i > maxIndex) break;
    maxIndex = Math.max(maxIndex, i + nums[i]);
    if (maxIndex >= lastIndex) return true;
  }
  return maxIndex >= lastIndex;
}

// console.log(canJump([2, 3, 1, 1, 4]) === true);
// console.log(canJump([3, 2, 1, 0, 4]) === false);
console.log(canJump([0]) === true);
